package stu.day25_day32.day26;
import stu.day25_day32.day25.all.*;

public class hw2 {
    int num=0;
    public int findTilt(TreeNode root) {
        int x = Tilt(root);
        return num;
    }

    public int Tilt(TreeNode root){
        if(root==null){
            return 0;
        }
        int left = Tilt(root.left);
        int right = Tilt(root.right);
        num += Math.abs(left-right);
        return left+right+root.val;
    }


    int max = 0;
    public int longestUnivaluePath(TreeNode root) {
        calMax(root);
        return max;
    }

    //val max
    public int calMax(TreeNode node){
        int arr = 0;
        if(node==null){
            return arr;
        }
        int left = calMax(node.left);
        int right = calMax(node.right);

        int l=0;
        int r=0;

        if(node.left!=null){
            if(node.val==node.left.val){
                l = left+1;
            }
        }
        if(node.right!=null){
            if(node.val==node.right.val){
                r = right+1;
            }
        }

        max = Math.max(max,l+r);

        arr=Math.max(l,r);
        return arr;
    }



}
